package com.kaka.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaka.pojo.Role;
import com.kaka.pojo.Users;
import com.kaka.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    UsersService usersService;
    @Autowired
    QnUpload qnUpload;

    @RequestMapping("/headImg")
    public String getUsername(HttpServletRequest request) {
        SecurityContext context = SecurityContextHolder.getContext();
        UserDetails userDetails = (UserDetails) context.getAuthentication().getPrincipal();
        Users users = usersService.findByUsersName(userDetails.getUsername());
        request.getSession().setAttribute("users", users);
        return "/main.jsp";
    }

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                          @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Users> usersList = usersService.findAll();
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);
        request.setAttribute("pageInfo", pageInfo);
        return "/users/users-list.jsp";
    }

    @GetMapping("/edit")
    public String edit(@RequestParam("id") Integer id, HttpServletRequest request) {
        Users users = usersService.findById(id);
        List<Role> roleList = usersService.findAllRole();
        request.setAttribute("users", users);
        request.setAttribute("roleList", roleList);
        return "/users/users-edit.jsp";
    }

    @PostMapping("/edit")
    public String edit(@RequestParam("files") MultipartFile files, Users users, int roleId) throws IOException {
        String fileName = UUID.randomUUID().toString() + files.getOriginalFilename();
        //files.transferTo(new File("D://IdeaProjects/Cloud-And-Data/Spring-SpringMVC-MyBatis/src/main/webapp/img", fileName));
        File file = new File("src/main/webapp/img/temp", fileName);
        files.transferTo(file);
        qnUpload.upload(file.getAbsolutePath(), fileName);
        users.setImgUrl(fileName);
        int i = usersService.edit(users, roleId);
        return "redirect:findAll";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam("id") Integer id) {
        int i = usersService.deleteUsers(id);
        return "redirect:findAll";
    }

    @GetMapping("/add")
    public String addPage(HttpServletRequest request) {
        List<Role> roleList = usersService.findAllRole();
        request.setAttribute("roleList", roleList);
        return "/users/users-add.jsp";
    }

    @PostMapping("/add")
    public String add(@RequestParam("files") MultipartFile files, Users users, int roleId) throws IOException {
        String fileName = UUID.randomUUID().toString() + files.getOriginalFilename();
        //files.transferTo(new File("D://IdeaProjects/Cloud-And-Data/Spring-SpringMVC-MyBatis/src/main/webapp/img", fileName));

        File file = new File("src/main/webapp/img/temp", fileName);
        files.transferTo(file);
        qnUpload.upload(file.getAbsolutePath(), fileName);
        users.setImgUrl(fileName);
        int i = usersService.addUsers(users, roleId);
        return "redirect:findAll";
    }
}
